﻿using System.Web;
using Grishko.SerilizeTools;
using Grishko.DataTier.SqlSentence;
using OutdoorLib.BaseEntity.Face;
using OutdoorLib.BaseEntity.Infrastructure;

namespace OutdoorLib.BaseEntity.Filters
{
    /// <summary>
    /// Список сотрудников 
    /// </summary>
    [TagNameInfo(TagName = "EmployeeListFilter")]
    public class EmployeeListFilter : Filter
    {

        public class EmplScriptEngine : ScriptEngine
        {
            public EmplScriptEngine(ScriptEngine.TScriptEngineSrchInfo Settings) : base(Settings) { }
            public override string GetScript()
            {
             
                if (null != HttpContext.Current.Request[qDepartmentID])
                {
                   MandatoryConditions .AddMandatoryCondition_Eq ("Department.ID", HttpContext.Current.Request[qDepartmentID]);

                }

                return base.GetScript();                
            }

        }

        public const string qDepartmentID = "DepartmentID";

        public override string ObjectPageKey
        {
            get
            {
                return "PageKey_EmployeeEditor";
            }
        }

        /// <summary>
        /// Добавить параметр - DepardamentID в строчку редактирования 
        /// </summary>
        /// <param name="sURl"></param>
        /// <returns></returns>
        public override string AddUrlHandler_Edit(string sURl)
        {
            string p = Grishko.Tools.QueryStringTools.GetParamValue(qDepartmentID);
            sURl = Grishko.Tools.QueryStringTools.GetModifyParamsRequest(sURl, qDepartmentID + "=" + p);
            return base.AddUrlHandler_Edit(sURl);
        }

        public override string AddUrlHandler_Insert(string sURl)
        {
            string p = Grishko.Tools.QueryStringTools.GetParamValue(qDepartmentID);
            sURl = Grishko.Tools.QueryStringTools.GetModifyParamsRequest(sURl, qDepartmentID + "=" + p);
            return base.AddUrlHandler_Insert(sURl);
        }

        public EmployeeListFilter()
        {
            ScriptEngine.TScriptEngineSrchInfo Settings = new ScriptEngine.TScriptEngineSrchInfo();
            Settings.EntityType = typeof(Employeer);
            Settings.SqlFunction = "GetEmployeeObjectPropertyStorage()";
            Settings.PropertyCollection.Add("Department/Департамент", typeof(OutdoorLib.BaseEntity.Agency.Department));
            Settings.BanProperties = new string[] { "Employees" };
            Where = new EmplScriptEngine(Settings);
        }
    }
}